/**
 * @file oled_demo.c
 * @brief SLE Client implementation for OLED display and AHT20 and HCSR04.
 *
 * This file is used to display temperature and humidity data, as well as ultrasonic sensor data, on an OLED screen and update it.
 *
 * @author Maker Studio
 * @license Apache 2.0
 *
 * This code is open-sourced under the Apache 2.0 license. When using open-source libraries
 * or code, please ensure to acknowledge the original authors and sources.
 */
#include <stdio.h>      // Standard input/output
#include <unistd.h>     // POSIX standard interface
#include "ohos_init.h"  // Used to initialize services and features
#include "cmsis_os2.h"  // CMSIS-RTOS API V2
#include "iot_gpio.h"   // OpenHarmony HAL: IoT hardware device operation interface - GPIO
#include "../inc/iot_gpio_ex.h"
#include "../inc/oled_ssd1306.h"// OLED display simplified driver interface
#include "../inc/aht20.h"     // AHT20 digital temperature and humidity sensor driver interface
#include "iot_errno.h"
#include"../inc/oled_fonts.h"

#define AHT20_BAUDRATE 400 * 1000 // Define macro for AHT20 baud rate (transmission speed)
#define AHT20_I2C_IDX 1          // Define macro for the I2C bus index, which is I2C1


// Main thread function
/**
 * @brief Task to manage OLED display and AHT20 sensor readings.
 *
 * This function initializes the necessary GPIO pins for I2C communication with the AHT20
 * humidity and temperature sensor. It then enters a continuous loop where it repeatedly
 * triggers measurements from the sensor, retrieves the data, and displays the temperature
 * and humidity values on the OLED screen. If initialization or measurement retrieval fails,
 * appropriate error messages are logged.
 *
 * @author Maker Studio
 * @license Apache 2.0
 */
void OledTask(void)
{
    uint32_t retval = 0;  // Return value from interfaces
    float humidity = 0.0f; // Humidity value
    float temperature = 0.0f; // Temperature value

    // Initialize GPIO
    IoTGpioInit(IOT_IO_NAME_GPIO_15);   // TXD1
    IoTGpioInit(IOT_IO_NAME_GPIO_16);   // RXD1

    IoSetFunc(IOT_IO_NAME_GPIO_15, IOT_IO_FUNC_GPIO_15_I2C1_SDA); // Set GPIO - Pin 15 function as I2C1_SDA
    IoSetFunc(IOT_IO_NAME_GPIO_16, IOT_IO_FUNC_GPIO_16_I2C1_SCL); // Set GPIO - Pin 16 function as I2C1_SCL

    IoTI2cInit(AHT20_I2C_IDX, AHT20_BAUDRATE); // Initialize I2C1 with specified baud rate
    initOledDisplayName();
}

/**
 * @brief Initialize OLED display with fixed labels.
 *
 * This function sets up an OLED display by initializing the display driver, filling the screen
 * with a black background, and displaying fixed text strings such as "Maker - Studio", "Tem:",
 * "Hum:", and "Distance:" in specific positions on the screen. This function is called once
 * to initialize the display before dynamic data updates begin.
 *
 * @author Maker Studio
 * @license Apache 2.0
 */
void initOledDisplayName(void)
{
    OledInit();                                     // Initialize SSD1306 display driver
    OledFillScreen(0x00);                           // Fill the screen with black color

    const char text1[] = "Maker - Studio";         // First line
    OledShowString(10, 0, text1, FONT6x8);          // Display string at position (10, 0)

    const char text2[] = "Tem:";                    // Second line
    OledShowString(0, 3, text2, FONT6x8);

    const char text3[] = "Hum:";                    // Third line
    OledShowString(0, 4, text3, FONT6x8);

}

/**
 * @brief Update OLED display with dynamic sensor data.
 *
 * This function updates the OLED display with the latest temperature and humidity readings.
 * It converts the float values of temperature and humidity to strings and displays them on
 * the OLED screen at predefined positions. The display is updated dynamically in each cycle
 * of the task.
 *
 * @param temperature The temperature value to display.
 * @param humidity The humidity value to display.
 *
 * @author Maker Studio
 * @license Apache 2.0
 */
void updateOledDataDisplay(float temperature, float humidity)
{
    char tempStr[10] = {0};
    char humiStr[10] = {0};
    sprintf(tempStr, "%.2f", temperature);
    sprintf(humiStr, "%.2f", humidity);
    OledShowString(40, 3, tempStr, FONT6x8);
    OledShowString(40, 4, humiStr, FONT6x8);
}
